package com.blb.controller;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.mapper.LaserMapper;
import com.blb.mapper.OrderMapper;
import com.blb.mapper.PrinterMapper;
import com.blb.service.ILaserService;
import com.blb.service.IUserService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;

import com.blb.service.IPrinterService;
import com.blb.service.impl.OrderServiceImpl;
import com.blb.utils.GetAutoCode;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-14
 */
@Controller
@RequestMapping("laser")
public class LaserController {
    @Resource
    private ILaserService is;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private LaserMapper laserMapper;

    @Resource
    private IPrinterService iPrinterService;

    @Resource
    private IUserService userService;

    /**
     * 页面跳转激光码
     * @return
     */

    @GetMapping("jgm")
    public String index02(){
        return "laser/sc_jgm";
    }

    /**
     * 页面跳转分配
     * @return
     */
    @GetMapping("fp")
    public String index03(){
        return "laser/sc_fp";
    }

    /**
     * 页面跳转自动分配
     * @return
     */
    @GetMapping("zd")
    public String index04(String ids, HttpSession request){


       request.setAttribute("ids",ids);

       return "laser/fp_zd";
    }

    /**
     * 自动分配页面
     * @param request
     * @param start
     * @param length
     * @return
     */
    @ResponseBody
    @RequestMapping("getserial")
    public BlbPage getserial(HttpSession request, int start, int length){

        String ids = (String) request.getAttribute("ids");

        System.out.println("ids"+ids);
        List<String> ids1 = Arrays.asList(ids.split(","));
        System.out.println("ids1"+ids1);
        Collection<Laser> lasers = is.listByIds(ids1);

        for (Laser laser1 : lasers){
            String pnum = is.findnumbyno(laser1.getLNo());
            is.updatebyno(pnum,GetAutoCode.getPipelineNumbers(),laser1.getLNo());
        }
        List<Laser> findbyid = is.findbyid(start, length, ids1);
        int count = is.count4(ids1);
        return new DataTablePage(count,count,findbyid);
    }


    /**
     * 激光码页面
     * @param start
     * @param length
     * @param orderid
     * @return
     */
     @ResponseBody
      @RequestMapping("all")
      public BlbPage getOrder(int start, int length, String orderid){
         User user = ShiroUtils.getUser();
         String companyId = user.getCompanyId();
         List<Order> orders = is.findbyorderId(start,length,orderid,companyId);
        int count = is.count(orderid,companyId);

        return new DataTablePage(count, count, orders);
   }

    /**
     * 批量删除
     * @param ids
     * @return
     */
   @ResponseBody
   @RequestMapping("delete")
 public ResponseHelp delete(@RequestBody String[] ids){

       for (String id: ids){
           is.delete(id);
       }
       ResponseHelp responseHelp = new ResponseHelp();
       return responseHelp;
 }




    /**
     * 生产激光码
     * @param id
     * @return
     */
      @ResponseBody
      @RequestMapping("lasers")
        public  ResponseHelp lasercode(String id){
          Order order = new Order();
          order.setId(id);
          order.setProductStatus("8");
          orderMapper.updateById(order);
          String companyId = ShiroUtils.getUser().getCompanyId();
          int num = is.findnumbyid(id);
          String no = is.findnobyid(id);
          for (int i = 0; i<num ;i++){
             Laser laser = new Laser();
              laser.setId(UUIDUtils.getId());
              laser.setLNo(no);
              laser.setLCode(GetAutoCode.getFourPipelineNumbers());
              laser.setCompanyId(companyId);
              laser.setLStatus("-1");
              is.addlaser(laser);
         }
          ResponseHelp responseHelp = new ResponseHelp();
          return responseHelp;
}


    /**
     * 激光码页面显示
     * @param start
     * @param length
     * @param lNo
     * @return
     */
    @ResponseBody
    @RequestMapping("findAllLaser")
    public BlbPage findAllLaser( Integer start, Integer length,String lNo){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();

        /*查询分页数据*/
        List<Order> orders = is.findbyorderId(start, length, lNo,companyId);
        System.out.println(lNo);
        /*查询总条数*/
        int count = is.count(lNo,companyId);
        return new DataTablePage(count,count,orders);
    }

    /**
     * 激光码弹窗显示
     * @param id
     * @param start
     * @param length
     * @return
     */
    @ResponseBody
    @RequestMapping("code")
     public  BlbPage findallcode(String id, Integer start, Integer length){

        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();

        String lNo = is.findnobyid(id);

        List<Laser> findbylon = is.findbylon(start, length, lNo,companyId);
        int count1 = is.count1(lNo,companyId);

        Order order = new Order();
        order.setLaserCode("1");
        order.setId(id);
        orderMapper.updateById(order);

         return new DataTablePage(count1,count1,findbylon);

    }


    /**
     * 分配页面显示
     * @param start
     * @param length
     * @param nocode
     * @return
     */
    @ResponseBody
    @RequestMapping("findfp")
    public BlbPage findfp( Integer start, Integer length,String nocode){

        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();


        /*查询分页数据*/
        List<Laser> findfp = is.findfp(start, length, nocode,companyId);

        /*查询总条数*/
        int count3 = is.count2(nocode,companyId);

        return new DataTablePage(count3,count3,findfp);
    }



    /**
     * 查询所有打印机
     * @param start
     * @param length
     * @return
     */
    @ResponseBody
    @RequestMapping("findprinter")
    public BlbPage findprinter( Integer start, Integer length){

        /*查询分页数据*/
        List<Printer> findallprinter = is.findallprinter(start, length);
        System.out.println("aa"+findallprinter);
        /*查询总条数*/
        int count3 = is.count3();
        System.out.println("count3"+count3);
        return new DataTablePage(count3,count3,findallprinter);
    }


    /**
     * 通过id删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deletecode")
   private ResponseHelp deletes(@RequestBody String[] ids){
        System.out.println(ids[0]);
        for(String id :ids){
        laserMapper.deleteById(id);
        }
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
   }



    @GetMapping("code")
    public String codelist() {
        return "quality_tracing/information";
      }


     @ResponseBody
     @RequestMapping("find")
     public ResponseHelp findcode(String code) {
        User user = ShiroUtils.getUser();
        userService.update(user.getId());
        User user2 = userService.getUserByUsername(user.getUserName());
        Laser byCode = is.findByCode(code, user.getCompanyId());
        ResponseHelp responseHelp = new ResponseHelp();
        if (byCode != null){
            byCode.setQtimes(user2.getQueryTimes());
            responseHelp.setData(byCode);
        }else {
            responseHelp.setData(new Laser());
        }
         return responseHelp;
    }


    /**
     * 自动打印
     * @param id1
     * @param id2
     * @return
     */
    @RequestMapping("dayin")
    private String dayin(String id1,String id2){
        Printer printer = iPrinterService.findbyid(id2);
        String pNum = printer.getPNum();
        Laser laser = new Laser();
       laser.setId(id1);
       laser.setLPrinter(pNum);
       laser.setLStatus("1");
       laserMapper.updateById(laser);
       return "redirect:/laser/fp";
    }

    /**
     * 点击确定更改状态
     * @param request
     * @return
     */
    @RequestMapping("queding")
     private String queding(HttpSession request){
        String ids = (String) request.getAttribute("ids");
          List<String> ids1 = Arrays.asList(ids.split(","));
          System.out.println("ids1"+ids1);
          is.updateStatus(ids1);
          return "redirect:/laser/fp";
      }

}

